<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/8/24
 * Time: 14:35
 */

namespace ticket\libs\Zhiyoubao;


use ticket\libs\Curl;
use ticket\libs\Tools;
use ticket\modules\frontend\models\Application;

class Configura
{
        protected $corpCode;
        protected $userName;
        protected $privateKey;
        protected $app_key;
        protected $aes_key;
        protected $aes_iv;
        protected $place_url;

    public function getApplication($aid)
    {

        $applicationModel = new Application();
        $applicationInfo = $applicationModel->getApplicationList($aid);

        if (!empty($applicationInfo)){
            if (!empty($applicationInfo['configuration'])){
                $configurationInfo = json_decode($applicationInfo['configuration'],true);
                $this->corpCode = isset($configurationInfo['corp_code'])?$configurationInfo['corp_code']:'';
                $this->userName = isset($configurationInfo['user_name'])?$configurationInfo['user_name']:'';
                $this->privateKey = isset($configurationInfo['private_key'])?$configurationInfo['private_key']:'';
                $this->place_url = isset($configurationInfo['place_url'])?$configurationInfo['place_url']:'';

            }
            $this->app_key = $applicationInfo['app_key'];
            $this->aes_key = $applicationInfo['aes_key'];
            $this->aes_iv = $applicationInfo['aes_iv'];
            return true;
        }
        return false;

    }

    /**** 门票下订单
     * @param $params
     * @return string
     */
    protected function getPlaceParams($params)
    {
        $xmlMsg = "<PWBRequest>
                        <transactionName>SEND_CODE_REQ</transactionName>                  
                        <header>
                            <application>SendCode</application>                             
                            <requestTime>".date("Y-m-d")."</requestTime>
                        </header>
                        <identityInfo>  
                            <corpCode>".$this->corpCode."</corpCode>                          
                            <userName>".$this->userName."</userName>                                  
                        </identityInfo>
                        <orderRequest>
                            <order>
                                <certificateNo>".$params['certificate']."</certificateNo>                
                                <linkName>".$params['name']."</linkName>                           
                                <linkMobile>".$params['phone']."</linkMobile>                      
                                <orderCode>".$params['order_sn']."</orderCode>         
                                <orderPrice>".$params['price']."</orderPrice>       
                                <groupNo></groupNo>
                                <payMethod>vm</payMethod>
                                <thirdPayMethod>weixin</thirdPayMethod >
                                <ticketOrders>
                                    <ticketOrder>
                                        <orderCode>".$params['order_sn']."</orderCode>       
                                        <price>".$params['unit_price']."</price>            
                                        <quantity>".$params['num']."</quantity>                
                                        <totalPrice>".$params['unit_price']."</totalPrice>
                                        <occDate>".$params['reserveTime']."</occDate>                     
                                        <goodsCode>".$params['ticket_sn']."</goodsCode>    
                                        <goodsName>".$params['goods_name']."</goodsName>         
                                        <remark>".$params['describe']."</remark>           
                                    </ticketOrder>
                                </ticketOrders>
                            </order>
                        </orderRequest> 
                    </PWBRequest>";


        $key = md5("xmlMsg=".$xmlMsg.",".$this->privateKey);
        $data = "xmlMsg=".$xmlMsg.",&sign=".$key;
        return $data;
    }


    /*** 获取门票二维码
     * @param $orderSn
     * @return string
     */
    protected function getQrcodeImg($orderSn)
    {
        $xmlMsg = "<PWBRequest>
                    <transactionName>SEND_CODE_IMG_REQ</transactionName>
                    <header>
                        <application>SendCode</application>
                        <requestTime>2011-21-20</requestTime>
                    </header>
                    <identityInfo>
                        <corpCode>".$this->corpCode."</corpCode>
                        <userName>".$this->userName."</userName>
                    </identityInfo>
                    <orderRequest>
                        <order>
                            <orderCode>".$orderSn."</orderCode>
                        </order>
                    </orderRequest>
                   </PWBRequest>";

        $key = md5("xmlMsg=".$xmlMsg.",".$this->privateKey);
        $params = "xmlMsg=".$xmlMsg.",&sign=".$key;
        return $params;

    }

    /*** 生成部分退票参数
     * @param $order_sn   子订单号
     * @param $ticketNum  退票数量
     * @param $refund_sn  第三方退单号
     * @return string
     */
    protected function getReturnTicket($order_sn,$ticketNum,$refund_sn,$id_number)
    {
        $xmlMsg = "<PWBRequest>
                        <transactionName>RETURN_TICKET_NUM_NEW_REQ</transactionName>
                        <header>
                            <application>SendCode</application>
                            <requestTime>".date("Y-m-d")."</requestTime>
                        </header>
                        <identityInfo>
                            <corpCode>".$this->corpCode."</corpCode>                          
                            <userName>".$this->userName."</userName>     
                        </identityInfo>
                        <orderRequest>
                            <returnTicket>
                                <orderCode>".$order_sn."</orderCode>
                                <returnNum>".$ticketNum."</returnNum>
                                <thirdReturnCode>".$refund_sn."</thirdReturnCode>
                                <idCards>".$id_number."</idCards>
                            </returnTicket>
                        </orderRequest>
                  </PWBRequest>";

        $key = md5("xmlMsg=".$xmlMsg.",".$this->privateKey);
        $params = "xmlMsg=".$xmlMsg.",&sign=".$key;
        return $params;
    }


    /*** 取消门票订单
     * @param $orderSn  订单号
     * @return string
     */
    protected function cancelOrder($orderSn)
    {
        $xmlMsg = "<PWBRequest>
                        <transactionName>SEND_CODE_CANCEL_NEW_REQ</transactionName>
                        <header>
                            <application>SendCode</application>
                            <requestTime>".date("Y-m-d")."</requestTime>
                        </header>
                        <identityInfo>
                            <corpCode>".$this->corpCode."</corpCode>                          
                            <userName>".$this->userName."</userName>     
                        </identityInfo>
                        <orderRequest>
                            <order>
                                <orderCode>".$orderSn."</orderCode>
                            </order>
                        </orderRequest>
                    </PWBRequest>";


        $key = md5("xmlMsg=".$xmlMsg.",".$this->privateKey);
        $params = "xmlMsg=".$xmlMsg.",&sign=".$key;
        return $params;
    }


    /*** 查询订单
     * @param $orderSn
     * @return string
     */
    public function queryOrder($orderSn)
    {
        $xmlMsg = "<PWBRequest>
                        <transactionName>QUERY_ORDER_NEW_REQ</transactionName>
                        <header>
                            <application>SendCode</application>
                            <requestTime>".date("Y-m-d")."</requestTime>
                        </header>
                            <identityInfo>
                                <corpCode>".$this->corpCode."</corpCode>                          
                                <userName>".$this->userName."</userName>   
                            </identityInfo>
                        <orderRequest>
                            <order>
                                <orderCode>".$orderSn."</orderCode>
                            </order>
                        </orderRequest>
                   </PWBRequest>";


        $key = md5("xmlMsg=".$xmlMsg.",".$this->privateKey);
        $params = "xmlMsg=".$xmlMsg.",&sign=".$key;
        return $params;
    }




    /*** 发送成功短信给用户,
     * @param $orderSn
     * @return string
     */
    protected function sendMessage($orderSn)
    {
        $xmlMsg = "<PWBRequest>
                        <transactionName>SEND_SM_REQ</transactionName>
                        <header>
                            <application>SendCode</application>
                            <requestTime>".date("Y-m-d")."</requestTime>
                        </header>
                        <identityInfo>
                            <corpCode>".$this->corpCode."</corpCode>                          
                            <userName>".$this->userName."</userName>     
                        </identityInfo>
                        <orderRequest>
                            <order>
                                <orderCode>".$orderSn."</orderCode>
                                <tplCode></tplCode>
                            </order>
                        </orderRequest>
                  </PWBRequest>";

        $key = md5("xmlMsg=".$xmlMsg.",".$this->privateKey);
        $params = "xmlMsg=".$xmlMsg.",&sign=".$key;
        return $params;
    }









    /*** AES-128-CBC，解密
     * @param $data   解密数据
     * @param $key    key
     * @param $iv     向量
     * @return string
     */
    protected function decryption($data,$key,$iv)
    {
        $res = openssl_decrypt($data,"AES-128-CBC",$key,0,$iv);
        return $res;
    }

    /**** 加密
     * @param $data
     * @param $key
     * @param $iv
     * @return string
     */
    protected function encrypt($data,$key,$iv)
    {
        $res =  openssl_encrypt($data, "AES-128-CBC", $key,0, $iv);
        return base64_encode($res);
    }


}